// 对应 售中-常规试剂价格申请单
import {ajax as commonAjax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
// js封装所需
function ajax(opt) {
	return new Promise((resolve, reject) => {
		let complete = (complete) => {
			if (opt.complete) {
				opt.complete(complete)
			}
			resolve(complete)
		}
		try{
			commonAjax({ ...opt, complete })
		}catch(e){
			reject()
		}
	})
}
//初始赋值
function  formonload(list,formData,bussiness){
	console.log("结果：", bussiness)
	let currentNodeid = bussiness.currentNodeid
	let uid = bussiness.docUnid
	// 暂存时加载客户信息数据
	let WF_DocStatus = bussiness.docStatus
	if(WF_DocStatus=="Draft"){
		fznext(formData, list);//暂存时加载客户信息数据
	}
	let arr = list.map(item => {
		if(formData.cCusName){
			//回退回来的表单客户信息不能修改
			if(['cCusCode','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
			'Cust_TYPE','cCusDefine2','OldDepart','CustomerTypeNo','CustomerTypeNoScore'].indexOf(item.name) != -1){
				item.disabled = true
			}
		}
		return item
	})
	return arr
}

async function formonsubmit(list,formData,bussiness){
	let user = getUserInfo()
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//验证客户编码是否存在
		let cCusCode = formData.cCusCode;
		if(cCusCode=="0"){
			uni.showToast({
				title: "【请填写客户编码！】",
				icon:'none'
			})
			return false;
		}
		
		let searchField = {
			paramasMap: {},
			headersMap: {
				sysid: "bpm",
				syspwd: "pass",
				userid: user.bpmUid
			},
			url: bpmUrl + '/rest/appform/getCustomerInfo'
		}
		// 验证客户名称和编码是否对应
		await ajax({
			url:'/bpm/get',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					cCusName: formData.cCusName,
					cCusCode: formData.cCusCode
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B078'
			} },
			success:res => {
				let a = res.data.data
				if (a==0) {
					uni.showToast({
						title: "【客户名称和编码有误，请检查后重新输入！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		// 验证子表信息不为空
		await ajax({
			url:'/bpm/get',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					docUnid: bussiness.docUnid
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B177'
			} },
			success:res => {
				let a = res.data.data
				if (a==0) {
					uni.showToast({
						title: "【请填写表体信息！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		// 验证数量字段不能为空
		await ajax({
			url:'/bpm/get',
			method: 'POST',
			data: { ...searchField, ...{
				paramasMap: {
					docUnid: bussiness.docUnid
				},
				url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B178'
			} },
			success:res => {
				let a = res.data.data
				if (a==1) {
					uni.showToast({
						title: "【请填写申请结算价！】",
						icon:'none'
					})
					return false;
				}
			}
		})
		
	}
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
		'Cust_TYPE','cCusDefine2','OldDepart','CustomerTypeNo','CustomerTypeNoScore'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}

//模糊搜索客户列表
function getCustomerName(queryString, formData,cb) {
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString,
			DepartmentID: formData.DepartmentID
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getCustomerInfo'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			let data = []
			if (res.data.data && res.data.data.length > 0) {
				res.data.data.map(item => {
					let temp = {}
					temp["value"] = item.cCusCode
					temp["name"] = item.cCusName
					temp["extraInfo"] = {
						comCode1: item.comCode1
					}
					data.push(temp)
				})
			} 
			cb(data)
		}
	})
}

//给相关字段赋值
async function fz(event, item,formData,list){
	let user = getUserInfo()
	// 客户名称相关赋值
	formData.cCusCode = event.value
	formData.comCode1 = event['extraInfo'].comCode1
	let searchField = {
		paramasMap: {},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + ''
	}
	// 提前disabled
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand',
		'Cust_TYPE','cCusDefine2','Invoice_Way','OldDepart','CustomerTypeNo','CustomerTypeNoScore'].indexOf(item.name) != -1){
			item.disabled = true
			return item
		}
	})
	// 省份
	// formData.Department = '北京'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getDepartmentByCondition'
		} },
		success:res => {
			// this.$set( formData, 'Department', res.data.data.Department )
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	// formData.cCusDefine6 = '类型1'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusDefine6'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	// 传真号码(cCusFax)
	// formData.cCusFax = '123'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusFax'
		} },
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	// 电话(cCusPhone)
	// formData.cCusPhone = '010'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusPhone'
		} },
		success:res => {
			formData.cCusPhone = res.data.data.cCusPhone
		}
	})
	// 手机(cCusHand)
	// formData.cCusHand = '131'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusHand'
		} },
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	// 客户类型(Cust_TYPE)
	// formData.Cust_TYPE = '客户类型'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B163'
		} },
		success:res => {
			formData.Cust_TYPE = res.data.data.Cust_TYPE
		}
	})
	// 开票类型(cCusDefine2)
	// formData.cCusDefine2 = '票据1'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getccdefine7'
		} },
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	// 开票方式(Invoice_Way)
	// formData.cCusDefine2 = '票据1'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B057'
		} },
		success:res => {
			formData.Invoice_Way = res.data.data.Invoice_Way
		}
	})
	// 根据被授权医院名称返回其所对应的大区
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				a: formData.Department,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
		} },
		success:res => {
			// formData.OldDepart = res.data.data
		}
	})
	// 获取客户档案内容
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				a: formData.Department,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B281'
		} },
		success:res => {
			formData.CustomerTypeNo = res.data.data.CustomerTypeNo
			formData.CustomerTypeNoScore = res.data.data.CustomerTypeNoScore
		}
	})
}

async function fznext(formData, list) {
	// 客户名称赋值
	let cCusName = formData.cCusName
	let searchField = {
		paramasMap: {},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + ''
	}
	// 提前disabled
	list = list.map(item => {
		if(['cCusCode','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand','Cust_TYPE',
		'cCusDefine2','Invoice_Way','OldDepart','CustomerTypeNo','CustomerTypeNoScore'].indexOf(item.name) != -1){
			item.disabled = true
			return item
		}
	})
	// 客户编号
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				cCusName: formData.cCusName
			},
			url: bpmUrl + '/rest/appform/appform/R_CRMVR_B115'
		} },
		success:res => {
			formData.cCusCode = res.data.data.cCusCode
		}
	});
	// 省份
	// formData.Department = '北京'
	await ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getDepartmentByCondition'
		} },
		success:res => {
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	// formData.cCusDefine6 = '类型1'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusDefine6'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	// 传真号码(cCusFax)
	// formData.cCusFax = '123'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusFax'
		} },
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	// 电话(cCusPhone)
	// formData.cCusPhone = '010'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusPhone'
		} },
		success:res => {
			formData.cCusDefine6 = res.data.data
		}
	})
	// 手机(cCusHand)
	// formData.cCusHand = '131'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getcCusHand'
		} },
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	// 客户类型(Cust_TYPE)
	formData.Cust_TYPE = '客户类型'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B163'
		} },
		success:res => {
			formData.Cust_TYPE = res.data.data.Cust_TYPE
		}
	})
	// 开票类型(cCusDefine2)
	formData.cCusDefine2 = '票据1'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				comCode: formData.comCode1,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/getccdefine7'
		} },
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	// 根据被授权医院名称返回其所对应的大区
	formData.ccdefine7 = '票据1'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				a: formData.Department,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
		} },
		success:res => {
			formData.OldDepart = res.data.data
		}
	})
	// 获取客户档案内容
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: { ...searchField, ...{
			paramasMap: {
				a: formData.Department,
				cCusCode: formData.cCusCode
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B281'
		} },
		success:res => {
			formData.CustomerTypeNo = res.data.data.CustomerTypeNo
			formData.CustomerTypeNoScore = res.data.data.CustomerTypeNoScore
		}
	})
}

export default {
	formonload,
	formonsubmit,
	getCustomerName,
	fz
}